package com.example.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.admin.common.result.R;
import com.example.admin.entity.SysRole;
import com.example.admin.entity.SysUser;
import com.example.admin.model.dto.RoleDTO;
import com.example.admin.model.dto.RoleRequest;
import com.example.admin.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("获取角色列表")
    @PostMapping("/list")
    public R<IPage<SysRole>> list(@RequestBody RoleRequest request) {
        return R.ok(roleService.getRolePage(request));
    }

    @ApiOperation("新增角色")
    @PostMapping("/add")
    public R<Void> add(@RequestBody RoleDTO roleDTO) {
        roleService.addRole(roleDTO);
        return R.ok();
    }

    @ApiOperation("更新角色")
    @PostMapping("/update")
    public R<Void> update(@RequestBody RoleDTO roleDTO) {
        roleService.updateRole(roleDTO);
        return R.ok();
    }

    @ApiOperation("删除角色")
    @GetMapping("/delete")
    public R<Void> delete(@RequestParam Long id) {
        roleService.deleteRole(id);
        return R.ok();
    }

    @ApiOperation("获取角色详情")
    @GetMapping("/get")
    public R<RoleDTO> getRole(@RequestParam Long id) {
        return R.ok(roleService.getRoleById(id));
    }

    @ApiOperation("获取角色关联的用户列表")
    @PostMapping("/{roleId}/users")
    public R<IPage<SysUser>> getRoleUsers(
        @PathVariable Long roleId,
        @RequestBody RoleRequest request
    ) {
        return R.ok(roleService.getRoleUsers(roleId, request));
    }

    @ApiOperation("更新角色菜单权限")
    @PostMapping("/{roleId}/menus")
    public R<Void> updateRoleMenus(
        @PathVariable Long roleId,
        @RequestBody List<Long> menuIds
    ) {
        roleService.updateRoleMenus(roleId, menuIds);
        return R.ok();
    }

    @ApiOperation("获取可以添加到角色的用户列表")
    @PostMapping("/{roleId}/available-users")
    public R<IPage<SysUser>> getAvailableUsers(
        @PathVariable Long roleId,
        @RequestBody RoleRequest request
    ) {
        request.setRoleId(roleId);
        return R.ok(roleService.getAvailableUsers(request));
    }

    @ApiOperation("添加用户到角色")
    @PostMapping("/{roleId}/users/add")
    public R<Void> addUsers(
        @PathVariable Long roleId,
        @RequestBody List<Long> userIds
    ) {
        roleService.addUsers(roleId, userIds);
        return R.ok();
    }

    @ApiOperation("从角色中移除用户")
    @DeleteMapping("/{roleId}/users/{userId}")
    public R<Void> removeUser(
        @PathVariable Long roleId,
        @PathVariable Long userId
    ) {
        roleService.removeUser(roleId, userId);
        return R.ok();
    }
} 